package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.ControlSignal;
import com.infoauto.opc.gateway.service.ControlSignalService;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 控制信号
 */

@CrossOrigin
@RestController
@RequestMapping("/controlSignal")
public class ControlSignalController {
    @Autowired
    private ControlSignalService service;

    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectControlSignal(String stationName,String projectName,String startTime,String endTime){
        try{
            List<ControlSignal> list = service.selectControlSignal(stationName,projectName,startTime,endTime);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(String stationName,String projectName,String startTime,String endTime, HttpServletResponse response) throws Exception {
        //1.构造数据
        List<ControlSignal> list = service.selectControlSignal(stationName,projectName,startTime,endTime);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.构造sheet
        String[] titles = {"编号", "工程名", "项目名","变化时间", "ON/OFF"};
        Sheet sheet = workbook.createSheet("控制信号列表");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        sheet.setColumnWidth(1, 20 * 256);//设置第二列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        Row row =sheet.createRow(0);

        AtomicInteger headersAi = new AtomicInteger();
        for (String title : titles) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(1);
        Cell cell = null;
        int i  = 1;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for (ControlSignal report : list) {
            Row dataRow = sheet.createRow(datasAi.getAndIncrement());
            //编号
            cell = dataRow.createCell(0);
            cell.setCellValue(i++);
            cell.setCellStyle(cellStyle);
            //工程名称
            cell = dataRow.createCell(1);
            cell.setCellValue(report.getStationName());
            cell.setCellStyle(cellStyle);
            //项目名称
            cell = dataRow.createCell(2);
            cell.setCellValue(report.getProjectName());
            cell.setCellStyle(cellStyle);
            //变化时间
            cell = dataRow.createCell(3);
            cell.setCellValue(formatter.format(report.getChangeTime()));
            cell.setCellStyle(cellStyle);
            //ON/OFF
            cell = dataRow.createCell(4);
            if(report.getStates()==0){
                cell.setCellValue("OFF");
                cell.setCellStyle(cellStyle);
            }else{
                cell.setCellValue("ON");
                cell.setCellStyle(cellStyle);
            }
        }
        String fileName = URLEncoder.encode(sheet.getSheetName()+formatter.format(new Date())+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }

}
